import datetime
import time
import random
from time import sleep
import modbus_tk.defines as cst
import modbus_tk.modbus_tcp as tcp

from modsim import change_modbus_value

ModsimIP = '172.26.172.209'
ModsimPort ='502'
ModsimType = 'FLOAT32'
# list = [100,102,105,101,98,97,104,106,107,103,99]
# def keepChange():
#     ModsimAddress = 32028
#     for i in range(1000):
#         for ModsimNumber in list:
#             sleep(10)
#             print(ModsimNumber)
#             change_modbus_value(ModsimIP, ModsimPort, ModsimAddress, ModsimType, ModsimNumber)

def quick_change_modbus_value():
    modbus_point_list=[32028,32030,32032,32034,8155]
    # modbus_point_list = [32028,32030]
    base_value = 200
    value_range = 10
    mult_quick_change_modbus_value(ModsimIP, ModsimPort, modbus_point_list, ModsimType, base_value, value_range)

# quick_change_modbus_value()
# keepChange()


def connection(modbus_ip, port_id):
    server = tcp.TcpMaster(modbus_ip, port_id)
    server.set_timeout(5.0)
    return server
def mult_quick_change_modbus_value(modbus_ip, port_id, modbus_point_list, data_type, base_value, value_range,reading_function_code="0X03",
                        is_thirdparty_equipment="0"):
    port_id = int(port_id)
    reading_function_code = reading_function_code.upper()
    data_type = data_type.upper()
    data_format = ""
    # print(base_value)

    server = connection(modbus_ip, port_id)
    if reading_function_code == "0X03":
        reading_function_code = cst.WRITE_MULTIPLE_REGISTERS
    elif reading_function_code == "0X01":
        reading_function_code = cst.WRITE_SINGLE_COIL
        data_format = ">i"
    else:
        print("Error")
    # if function_code or data_format is wrong, the server execute will fail
    # starting_address = int(starting_address) - 1
    i = 0
    while i < 500:
        for modbus_point in modbus_point_list:
            starting_address = int(modbus_point) - 1
            point_value = base_value + random.uniform(0, value_range)
            # print(point_value)
            if data_type == "FLOAT32":
                data_format = ">f"
                point_value = [float(point_value)]
            elif data_type == "DOUBLE":
                data_format = ">d"
                point_value = [float(point_value)]
            elif data_type in ("INT8", "INT16", "UINT8", "UINT16", "MOD10000", "BOOL"):
                data_format = ">H"
                if '.' in point_value:
                    point_value = (str(point_value).split('.')[0])
                point_value = [int(point_value)]
            elif data_type in ("INT32", "UINT32"):
                data_format = ">i"
                point_value = [int(point_value)]
            elif data_type in ("INT64", "UINT64"):
                data_format = ">ll"
                point_value = [0, int(point_value)]
            else:
                print("Error")

            try:
                start = datetime.datetime.now()
                server.execute(slave=1, function_code=reading_function_code, starting_address=starting_address,
                               output_value=point_value, data_format=data_format)
                end = datetime.datetime.now()
                costtime = end - start
                print("【Info】 Cost time of changing modbus value {}".format(costtime))
            except Exception as e:
                print("Exception:", e, "Please check parameters: data_format and reading_function_code")
        # i += 1
    server.close()

quick_change_modbus_value()